package net.fornwall.eclipsecoder.csharpsupport;

import net.fornwall.eclipsecoder.util.Utilities;

import org.eclipse.jface.preference.IPreferencePageContainer;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.emonic.base.FileManipulators.BuildfileManipulatorFactory;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>,
 * we can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * 
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */
public class CSharpPreferencesPage implements IWorkbenchPreferencePage {
	private Text codeTemplateEditor;

	private Label buildSystemLabel;

	private Label targetFrameworkLabel;

	private Label codeTemplateLabel;

	private Composite composite;

	public Point computeSize() {
		return composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
	}

	private Combo buildSystemCombo;

	private Combo targetFrameworkCombo;

	public void createControl(Composite parent) {
		composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));

		buildSystemLabel = new Label(composite, SWT.NONE);
		buildSystemLabel.setText("Build system:");

		buildSystemCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		buildSystemCombo.setItems(new String[] { "ant", "nant" });
		buildSystemCombo.setText(CSharpSupportPlugIn.getInstance().getBuildSystem());
		buildSystemCombo.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
		buildSystemCombo.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				targetFrameworkCombo.setItems(BuildfileManipulatorFactory.createDummyBFM(buildSystemCombo.getText())
						.getSupportedFrameworks());
			}

		});

		targetFrameworkLabel = new Label(composite, SWT.NONE);
		targetFrameworkLabel.setText("Target framework:");

		targetFrameworkCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		targetFrameworkCombo.setItems(BuildfileManipulatorFactory.createDummyBFM(buildSystemCombo.getText())
				.getSupportedFrameworks());
		targetFrameworkCombo.setText(CSharpSupportPlugIn.getInstance().getTargetFramework());
		targetFrameworkCombo.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));

		codeTemplateLabel = new Label(composite, SWT.NONE);
		codeTemplateLabel.setText("Code template:");

		codeTemplateEditor = new Text(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		codeTemplateEditor.setFont(JFaceResources.getFont(JFaceResources.TEXT_FONT));
		codeTemplateEditor.setTabs(5);
		codeTemplateEditor.setText(CSharpSupportPlugIn.getInstance().getCodeTemplate());
		codeTemplateEditor.setLayoutData(new GridData(GridData.FILL_BOTH));
	}

	public void dispose() {
		composite.dispose();
	}

	public Control getControl() {
		return composite;
	}

	public String getDescription() {
		return getTitle();
	}

	public String getErrorMessage() {
		// null to indicate no error message
		return null;
	}

	public Image getImage() {
		return null;
	}

	public String getMessage() {
		return null;
	}

	public String getTitle() {
		return "EclipseCoder C# Preferences";
	}

	public void init(IWorkbench workbench) {
		// from IWorkbenchPreferencePage interface
	}

	public boolean isValid() {
		return true;
	}

	public boolean okToLeave() {
		return true;
	}

	public boolean performCancel() {
		return true;
	}

	public void performHelp() {
		// do nothing
	}

	public boolean performOk() {
		if ("nant".equals(buildSystemCombo.getText()) && "".equals(targetFrameworkCombo.getText())) {
			Utilities.showMessageDialog("No target framework specified",
					"A target framework needs to be specified for nant to work.", true);
			return false;
		}

		IPreferenceStore preferenceStore = CSharpSupportPlugIn.getInstance().getPreferenceStore();
		preferenceStore.setValue(CSharpSupportPlugIn.CODE_TEMPLATE_PREFERENCE, codeTemplateEditor.getText());
		preferenceStore.setValue(CSharpSupportPlugIn.BUILD_SYSTEM_PREFERENCE, buildSystemCombo.getText());
		preferenceStore.setValue(CSharpSupportPlugIn.TARGET_FRAMEWORK_PREFERENCE, targetFrameworkCombo.getText());

		return true;
	}

	public void setContainer(IPreferencePageContainer preferencePageContainer) {
		// do nothing
	}

	public void setDescription(String description) {
		// do nothing
	}

	public void setImageDescriptor(ImageDescriptor image) {
		// do nothing
	}

	public void setSize(Point size) {
		composite.setSize(size);
	}

	public void setTitle(String title) {
		// do nothing
	}

	public void setVisible(boolean visible) {
		composite.setVisible(visible);
	}

}